import sys
import cv2
import numpy as np
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton

class VideoPlayer(QWidget):
    def __init__(self):
        super().__init__()

        # 初始化界面元素
        self.setWindowTitle("视频播放 + 放大镜")
        self.setGeometry(100, 100, 800, 600)

        self.video_path = r'D:\tennis\videos\line_videos\done\1734661378089.h264'  # 设置你的视频路径
        self.capture = cv2.VideoCapture(self.video_path)

        self.video_label = QLabel(self)
        self.video_label.setFixedSize(1280, 720)

        self.magnifier = QLabel(self)
        self.magnifier.setFixedSize(100, 100)
        self.magnifier.setStyleSheet("border: 3px solid black;")

        self.play_button = QPushButton('播放/暂停', self)
        self.play_button.clicked.connect(self.toggle_play)

        layout = QVBoxLayout()
        layout.addWidget(self.video_label)
        layout.addWidget(self.magnifier)
        layout.addWidget(self.play_button)

        self.setLayout(layout)

        # 定时器每40ms更新一次视频帧
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_frame)

        # 播放状态
        self.is_playing = False

        # 放大镜位置
        self.magnifier_x = 0
        self.magnifier_y = 0

    def toggle_play(self):
        if self.is_playing:
            self.timer.stop()
        else:
            self.timer.start(80)  # 每40ms更新一次
        self.is_playing = not self.is_playing

    def update_frame(self):
        ret, frame = self.capture.read()
        if not ret:
            self.capture.set(cv2.CAP_PROP_POS_FRAMES, 0)
            ret, frame = self.capture.read()

        # 转换颜色格式
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # 将帧转换为 QImage，再转换为 QPixmap
        height, width, _ = frame.shape
        qimg = QImage(frame.data, width, height, 3 * width, QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qimg)

        # 显示视频帧
        self.video_label.setPixmap(pixmap.scaled(self.video_label.size(), Qt.KeepAspectRatio))

        # 更新放大镜
        self.update_magnifier(frame)

    def update_magnifier(self, frame):
        # 从视频帧获取放大镜的区域
        magnifier_region = frame[self.magnifier_y:self.magnifier_y+100, self.magnifier_x:self.magnifier_x+100]

        # 放大镜显示
        magnifier_region = cv2.resize(magnifier_region, (100, 100), interpolation=cv2.INTER_LINEAR)
        height, width, _ = magnifier_region.shape
        qimg = QImage(magnifier_region.data, width, height, 3 * width, QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qimg)

        # 显示放大镜
        self.magnifier.setPixmap(pixmap)

    def mouseMoveEvent(self, event):
        # 更新放大镜位置
        self.magnifier_x = event.x() - self.magnifier.width() // 2
        self.magnifier_y = event.y() - self.magnifier.height() // 2

        # 确保放大镜位置不会超出视频显示范围
        self.magnifier_x = max(0, min(self.magnifier_x, self.video_label.width() - self.magnifier.width()))
        self.magnifier_y = max(0, min(self.magnifier_y, self.video_label.height() - self.magnifier.height()))

        # 更新放大镜显示
        self.update_magnifier(self.capture.read()[1])

    def closeEvent(self, event):
        self.capture.release()
        event.accept()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    player = VideoPlayer()
    player.show()
    sys.exit(app.exec_())
